//
//  DHRank&CommentTableViewCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/13.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHRank&CommentTableViewCell.h"
#import "DHRank_CommentCollectionCell.h"
#import "DHCommunityDetailController.h"

@interface DHRank_CommentTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *itemArr; //item数组

@end

@implementation DHRank_CommentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.moreButton.layer.cornerRadius = self.moreButton.height / 2;
    
    WeakSelf(weakSelf)
    [self.changeButton bk_addEventHandler:^(id sender) {
        [weakSelf routerEventWithName:@"change_dianping" userInfo:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHRank_CommentCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHRank_CommentCollectionCell];
}

- (NSArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)updateCellWithArray:(NSArray *)arr{
    self.itemArr = arr;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHRank_CommentCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHRank_CommentCollectionCell forIndexPath:indexPath];
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    if ([[model objectForKey:@"images"] count] > 0) {
        [cell.imageView dh_setImageURLString:[[model objectForKey:@"images"] objectAtIndex:0]];
    }else{
        [cell.imageView setImage:[UIImage imageNamed:@"img_default.jpg"]];
    }
    cell.likeLabel.text = [NSString stringWithFormat:@"%@",[model objectForKey:@"nice_count"]];
    [cell.headImageView dh_setImageURLString:[[model objectForKey:@"owner"] objectForKey:@"photo"]];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 16 - 8)/2,230};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHCommunityDetailController *vc = [[DHCommunityDetailController alloc] initWithCommunityDic:model];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
